package com.yilin.medical.activity;

import java.lang.reflect.Type;
import java.util.Map;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.yilin.medical.R;
import com.yilin.medical.http.AsyTask;
import com.yilin.medical.lsh.BaseResult;
import com.yilin.medical.lsh.Http;
import com.yilin.medical.lsh.Post4_3;
import com.yilin.medical.lsh.Post5_1;
import com.yilin.medical.lsh.Post5_15;
import com.yilin.medical.lsh.Regist;
import com.yilin.medical.utils.Inject;
import com.yilin.medical.utils.InjectUtils;
import com.yilin.medical.utils.PromptManager;

public class RegisterActivity extends BaseActivity {

	@Inject(R.id.et_phone)
	private EditText et_phone;
	@Inject(R.id.et_verifycode)
	private EditText et_verifycode;

	@Inject(R.id.tv_getverifycode)
	private TextView tv_getverifycode;
	@Inject(R.id.et_psword)
	private EditText et_psword;

	@Inject(R.id.et_psword_confirm)
	private EditText et_psword_confirm;
	@Inject(R.id.agree)
	private CheckBox agree;

	@Inject(R.id.protocol)
	private TextView protocol;
	@Inject(R.id.tv_register)
	private TextView tv_register;
	@Inject(R.id.rl_xieyi)
	private View rl_xieyi;
	// 是注册还是 忘记密码
	private boolean isReg;
	public static final String REG = "Register";
	private long time = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentViewWithTitle(R.layout.activity_register);
		InjectUtils.inject(this);
		isReg = getIntent().getBooleanExtra(REG, false);
		initView();
	}

	private void initView() {
		mTitle.setTitleRight("登录", View.VISIBLE, this);
		if (isReg) {
			mTitle.getTitle().setText("注册");
			// mTitle.setTitleRight("登录", View.VISIBLE, this);
		} else {
			mTitle.getTitle().setText("忘记密码");
			rl_xieyi.setVisibility(View.INVISIBLE);

			tv_register.setText("确定");

		}

		tv_getverifycode.setOnClickListener(this);
		protocol.setOnClickListener(this);
		tv_register.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.title_tv:
			startAct(LoginActivity.class, true);
			break;
		case R.id.tv_getverifycode:
			// 获取验证码接口
			getCode();
			break;
		case R.id.tv_login:
			// TODO 走登录接口
			loginFindPsw();
			break;
		case R.id.protocol:
			// TODO 走登录接口
			// 开启网页到用户协议界面
			Uri uri = Uri.parse("http://www.baidu.com");
			Intent it = new Intent(Intent.ACTION_VIEW, uri);
			startActivity(it);
			break;

		default:
			break;
		}
	}

	@SuppressWarnings("unchecked")
	private void loginFindPsw() {
		String num = et_phone.getText().toString().trim();
		String smsCod = et_verifycode.getText().toString().trim();
		String psw = et_psword.getText().toString().trim();
		String repsw = et_psword_confirm.getText().toString().trim();
		if (TextUtils.isEmpty(num) || TextUtils.isEmpty(num)
				|| TextUtils.isEmpty(num) || TextUtils.isEmpty(num)) {
			PromptManager.showToast(this, "用户名,验证码,密码不能为空");
			return;
		}
		if (!psw.equals(repsw)) {
			PromptManager.showToast(this, "两次密码不一致");
			return;
		}

		String url;
		Map<String, Object> params = null;
		if (isReg) {
			url = Http.HttpApis.regist;
			Post5_1 post = new Post5_1();
			post.setPassword(psw);
			post.setUsername(num);
			post.setSmscode(smsCod);
		} else {
			url = Http.HttpApis.setPassword;
			Post5_15 post = new Post5_15();
			post.setNewpassword(psw);
			post.setNewpasswordRe(repsw);
			post.setSmscode(smsCod);
			post.setUsername(num);
		}

		Type type = new TypeToken<BaseResult<Regist>>() {
		}.getType();
		new AsyTask<BaseResult<Regist>>(RegisterActivity.this, url, type, true) {

			@Override
			protected void onPost(BaseResult<Regist> result) {
				// TODO Auto-generated method stub
				removeProgressDialog();
				if (result.getSimpleException() != null) {
					PromptManager.showToast(RegisterActivity.this, result
							.getSimpleException().getMessage());
				} else {
					if (result.getCode() != 0) {
						PromptManager.showToast(RegisterActivity.this,
								result.getText());
					} else {
							PromptManager.showToast(RegisterActivity.this, "请登录");
				
					}
				}
			}
		}.execute(params);

	}

	// 获取验证码
	@SuppressWarnings("unchecked")
	private void getCode() {
		if (System.currentTimeMillis() - time > 60 * 1000) {
			String num = et_phone.getText().toString();
			Post4_3 post = new Post4_3();
			post.setMobile(num);
			post.setType((isReg == true ? 1 : 2));

			Map<String, Object> params = post.toMap(post);

			Type type = new TypeToken<BaseResult<Object>>() {
			}.getType();
			new AsyTask<BaseResult<Object>>(RegisterActivity.this,
					Http.HttpApis.sendSmsCode, type, true) {

				@Override
				protected void onPost(BaseResult<Object> result) {
					// TODO Auto-generated method stub
					removeProgressDialog();
					if (result.getSimpleException() != null) {
						PromptManager.showToast(RegisterActivity.this, result
								.getSimpleException().getMessage());
					} else {
						if (result.getCode() != 0) {
							PromptManager.showToast(RegisterActivity.this,
									result.getText());
						}else{
							if(isReg){
								PromptManager.showToast(RegisterActivity.this, "验证码正在路上");
							}else{
								PromptManager.showToast(RegisterActivity.this, "密码修改成功");
							}
						}
					}
				}
			}.execute(params);
		} else {
			PromptManager.showToast(this, "请60秒后再试");
		}

	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

}
